package com.qf.controller;

import com.qf.entity.*;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.OrderDetailService;
import com.qf.service.OrderService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.OrderDetailServiceImpl;
import com.qf.service.impl.OrderServiceImpl;
import com.qf.utils.RandomUtils;
import com.qf.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author wgy
 * @version V1.0
 * @project gp12_xiaomi
 * @package ${PACKAGE_NAME}
 * @description: ${TODO}
 * @date 2022/6/22 11:14
 */
@WebServlet(name = "OrderServlet",urlPatterns = "/orderservlet")
public class OrderServlet extends BaseServlet {
    //获取下订单信息
    public String getOrderView(HttpServletRequest request,HttpServletResponse response) throws Exception{
        //1判断是否登录
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/index.jsp";
        }
        //2创建业务对象
        //2.1 购物车业务对象
        CartService cartService=new CartServiceImpl();
        //2.2 收货地址业务对象
        AddressService addressService=new AddressServiceImpl();
        try {
            //3调用方法
            List<Cart> cartList = cartService.findByUid(user.getId());
            List<Address> addList=addressService.findByUid(user.getId());
            //4放入域中
            request.setAttribute("cartList", cartList);
            request.setAttribute("addList", addList);
            //5 转发
            return "/order.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","结账失败:"+e.getMessage() );
            return "/message.jsp";
        }
    }

    //添加订单
    public String addOrder(HttpServletRequest request,HttpServletResponse response){
        System.out.println("添加订单");
        //1判断是否登录
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/index.jsp";
        }
        //2获取收货地址
        String aid = request.getParameter("aid");
        if(StringUtils.isEmpty(aid)){
            request.setAttribute("msg","地址不能为空");
            return "/message.jsp";
        }
        //3获取购物车信息
        CartService cartService=new CartServiceImpl();
        List<Cart> cartList = cartService.findByUid(user.getId());
        if(cartList==null||cartList.size()==0){
            request.setAttribute("msg","购物车不能为空");
            return "/message.jsp";
        }
        //4创建订单对象
        //生成订单号
        String oid = RandomUtils.createOrderId();
        //计算总金额
        BigDecimal sum=new BigDecimal(0);
        for (Cart cart : cartList) {
            sum=sum.add(cart.getMoney());
        }
        //5提交订单
        OrderService orderService=new OrderServiceImpl();
        try {
            Order order=new Order(oid, user.getId() , sum, "1", new Date(), Integer.parseInt(aid));
            orderService.submitOrder(order,cartList);
            request.setAttribute("order",order);
            return "/orderSuccess.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","提交订单失败");
            return "/message.jsp";
        }
    }

    //获取订单列表
    public String getOrderList(HttpServletRequest request,HttpServletResponse response) throws Exception{
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/index.jsp";
        }
        //1创建业务对象
        OrderService orderService=new OrderServiceImpl();
        try {
            //2调用方法
            List<Order> orderList=orderService.findByUid(user.getId());
            //3把orderList放入域中
            request.setAttribute("orderList", orderList);
            //4转发
            return "/orderList.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","获取订单列表失败");
            return "/message.jsp";
        }
    }


    //获取订单详情
    public String getOrderDetail(HttpServletRequest request,HttpServletResponse response) throws Exception{
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/index.jsp";
        }
        //1获取订单号
        String oid = request.getParameter("oid");
        if(StringUtils.isEmpty(oid)){
            return "redirect:/orderservlet?method=getOrderList";
        }
        //2创建业务对象
        OrderService orderService=new OrderServiceImpl();
        OrderDetailService orderDetailService=new OrderDetailServiceImpl();
        //3获取订单和订单详情
        try {
            Order order=orderService.findByOid(oid);
            List<OrderDetail> orderDetailList=orderDetailService.findByOid(oid);
            //4放入域中
            request.setAttribute("order",order);
            request.setAttribute("orderDetailList", orderDetailList);
            return "/orderDetail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","查询失败:"+e.getMessage());
            return "/message.jsp";
        }
    }
    //确定收货
    public String changeStatus(HttpServletRequest request,HttpServletResponse response) throws Exception{
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/index.jsp";
        }
        //获取订单编号
        String oid = request.getParameter("oid");
        if(StringUtils.isEmpty(oid)){
            return "redirect:/orderservlet?method=getOrderList";
        }
        //创建业务对象
        OrderService orderService=new OrderServiceImpl();
        //调用方法
        try {
            orderService.updateStatus(oid, "4");
            return "redirect:/orderservlet?method=getOrderList";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","确认收货失败" );
            return "/mesage.jsp";
        }
    }
}
